// 3356. 零数组变换 II

// int[] nums = { 5 };
// int[][] queries = { [0, 0, 5], [0, 0, 1], [0, 0, 3], [0, 0, 2] };
 
// s.MinZeroArray(nums, queries);

public partial class Solution
{
    public int MinZeroArray(int[] nums, int[][] queries)
    {
        var arr = new int[nums.Length + 1];
        var (cur, count) = (0, -1);

        foreach (var item in nums)
        {
            if (item != 0)
            {
                count = 0;
                break;
            }
        }

        if (count != 0)
            return 0;

        for (int i = 0; i < queries.Length; i++)
        {
            var (left, right, value) = (queries[i][0], queries[i][1], queries[i][2]);
            arr[left] += value;
            arr[right + 1] -= value;

            if (left <= cur && cur <= right)
                count += value;

            while (cur < nums.Length && count >= nums[cur])
                count += arr[++cur];

            if (cur == nums.Length)
                return i + 1;
        }

        return -1;
    }
}